<!DOCTYPE HTML>
<!--
	Dimension by HTML5 UP
	html5up.net | @ajlkn
	Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
 <head>
  <title>
   Dimension by HTML5 UP
  </title>
  <!-- <meta charset="utf-8" /> -->
  <!-- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> -->
  <meta charset="utf-8"/>
  <meta content="width=device-width,initial-scale=1.0" name="viewport"/>
  <link href="../../assets/css/article.css" rel="stylesheet"/>
  <link href="https://cdn.bootcss.com/highlight.js/9.15.8/styles/github.min.css" rel="stylesheet"/>
  <noscript>
   <link href="../../assets/css/noscript.css" rel="stylesheet"/>
  </noscript>
 </head>
 <body>
  <div id="app">
  </div>
  <!-- built files will be auto injected -->
 </body>
 <body class="is-preload">
  <!-- Wrapper -->
  <div id="wrapper">
   <!-- Main -->
   <div id="main">
    <article id="article">
     <h1 id="redis">
      Redis 主从复制 + 高可用
     </h1>
     <hr/>
     <h2 id="_1">
      简介
     </h2>
     <p>
      使用docker配置Redis主从复制，配置 sentinel 实现高可用，一主两从
     </p>
     <p>
      <em>
       这里就写一个大概，比较好的，可以参考后面的链接，进行更详细，完整的设置
      </em>
     </p>
     <h2 id="_2">
      配置记录
     </h2>
     <h3 id="redis_1">
      Redis主从复制
     </h3>
     <p>
      准备配置文件，分别启动启动在6380/6381/6382,6380为主
     </p>
     <div class="codehilite">
      <pre><span></span><code>port <span class="m">6379</span>
<span class="nb">bind</span> <span class="m">192</span>.168.11.202
requirepass <span class="s2">"myredis"</span>
daemonize yes
logfile <span class="s2">"6379.log"</span>
dbfilename <span class="s2">"dump-6379.rdb"</span>
dir <span class="s2">"/opt/soft/redis/data"</span>
</code></pre>
     </div>
     <div class="codehilite">
      <pre><span></span><code>docker run -dit --name redis1 -p <span class="m">6381</span>:6379 -p <span class="m">16381</span>:16381 redis 
docker run -dit --name redis2 -p <span class="m">6382</span>:6379 -p <span class="m">16382</span>:16382 redis 
docker run -dit --name redis3 -p <span class="m">6383</span>:6379 -p <span class="m">16383</span>:16383 redis 


docker <span class="nb">exec</span> -ti redis2 redis-cli

<span class="m">127</span>.0.0.1:6379&gt; REPLICAOF <span class="m">192</span>.168.101.104 <span class="m">6381</span>
OK

<span class="m">127</span>.0.0.1:6379&gt; info replication
<span class="c1"># Replication</span>
role:slave
master_host:192.168.101.104
master_port:6381
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:126
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:3003c01fb5c96132dbc1e366cdc94b214a0ac52d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:126
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:126


docker <span class="nb">exec</span> -ti redis3 redis-cli

<span class="m">127</span>.0.0.1:6379&gt; REPLICAOF <span class="m">192</span>.168.101.104 <span class="m">6381</span>
OK

<span class="m">127</span>.0.0.1:6379&gt; info replication
<span class="c1"># Replication</span>
role:slave
master_host:192.168.101.104
master_port:6381
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:98
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:3003c01fb5c96132dbc1e366cdc94b214a0ac52d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:98
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:71
repl_backlog_histlen:28


docker <span class="nb">exec</span> -ti redis1 redis-cli

<span class="m">127</span>.0.0.1:6379&gt; info replication
<span class="c1"># Replication</span>
role:master
connected_slaves:2
slave0:ip<span class="o">=</span><span class="m">172</span>.17.0.1,port<span class="o">=</span><span class="m">6379</span>,state<span class="o">=</span>online,offset<span class="o">=</span><span class="m">140</span>,lag<span class="o">=</span><span class="m">0</span>
slave1:ip<span class="o">=</span><span class="m">172</span>.17.0.1,port<span class="o">=</span><span class="m">6379</span>,state<span class="o">=</span>online,offset<span class="o">=</span><span class="m">140</span>,lag<span class="o">=</span><span class="m">0</span>
master_replid:3003c01fb5c96132dbc1e366cdc94b214a0ac52d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:140
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:140
</code></pre>
     </div>
     <h3 id="sentinel">
      配置 Sentinel 哨兵
     </h3>
     <p>
      sentinel配置文件如下：
     </p>
     <div class="codehilite">
      <pre><span></span><code>port <span class="m">16381</span>
daemonize no
pidfile /var/run/redis-sentinel.pid
logfile <span class="s2">""</span>
dir /tmp
sentinel monitor mymaster <span class="m">192</span>.168.101.104 <span class="m">6381</span> <span class="m">2</span>
sentinel down-after-milliseconds mymaster <span class="m">10000</span>
sentinel failover-timeout mymaster <span class="m">30000</span>
sentinel parallel-syncs mymaster <span class="m">1</span>
</code></pre>
     </div>
     <p>
      命令运行记录如下：
     </p>
     <div class="codehilite">
      <pre><span></span><code>docker cp sentinel.conf redis1:/etc/sentinel.conf

docker <span class="nb">exec</span> -ti redis1 redis-sentinel /etc/sentinel.conf

<span class="m">44</span>:X <span class="m">03</span> Jan <span class="m">2021</span> <span class="m">06</span>:39:16.506 <span class="c1"># +monitor master mymaster 192.168.101.104 6381 quorum 2</span>
<span class="m">44</span>:X <span class="m">03</span> Jan <span class="m">2021</span> <span class="m">06</span>:39:16.512 * +slave slave <span class="m">172</span>.17.0.1:6379 <span class="m">172</span>.17.0.1 <span class="m">6379</span> @ mymaster <span class="m">192</span>.168.101.104 <span class="m">6381</span>
<span class="m">44</span>:X <span class="m">03</span> Jan <span class="m">2021</span> <span class="m">06</span>:39:26.666 * +convert-to-slave slave <span class="m">172</span>.17.0.1:6379 <span class="m">172</span>.17.0.1 <span class="m">6379</span> @ mymaster <span class="m">192</span>.168.101.104 <span class="m">6381</span>
</code></pre>
     </div>
     <h2 id="_3">
      参考链接
     </h2>
     <ul>
      <li>
       <a href="https://hub.docker.com/_/redis?tab=description&amp;page=1&amp;ordering=last_updated">
        Docker Official Images
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/huanghuitan/article/details/108044983">
        六、Redis 主从复制 Replicaof、哨兵 Sentinel
       </a>
      </li>
      <li>
       <a href="https://juejin.cn/post/6844903984294002701">
        redis cluster集群模式原理
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/baidu_27627251/article/details/112143714">
        Windows下部署redis主从、哨兵（sentinel）、集群（cluster）
       </a>
      </li>
      <li>
       <a href="https://github.com/Johar77/JAVA-000/tree/main/Week_12">
        配置 redis 的主从复制，sentinel 高可用，Cluster 集群
       </a>
      </li>
     </ul>
    </article>
   </div>
   <!-- Footer -->
   <footer id="footer">
    <p class="copyright">
     © Untitled. Design:
     <a href="https://html5up.net">
      HTML5 UP
     </a>
     .
    </p>
   </footer>
  </div>
  <!-- BG -->
  <div id="bg">
  </div>
  <!-- Scripts -->
  <script src="../assets/js/jquery.min.js">
  </script>
  <script src="../assets/js/browser.min.js">
  </script>
  <script src="../assets/js/breakpoints.min.js">
  </script>
  <script src="../assets/js/util.js">
  </script>
  <script src="../assets/js/main.js">
  </script>
 </body>
</html>
